package cloud.alibaba.study.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cloud.alibaba.study.dao.order.OrderDao;
import cloud.alibaba.study.entity.base.ResponseVo;
import cloud.alibaba.study.entity.order.Order;
import cloud.alibaba.study.service.IAccountService;
import cloud.alibaba.study.service.IOrderService;
import cloud.alibaba.study.service.IStorageService;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class OrderServiceImpl implements IOrderService {

	@Autowired
	private OrderDao orderDao;
	@Autowired
	private IStorageService storageService;
	@Autowired
	private IAccountService accountService;

	@Override
	public ResponseVo createOrder(Order order) {
		log.info("------->下单开始");
		// 下订单
		orderDao.createOrder(order);
		log.info("------->order-service中扣减库存结束");
		// 减库存
		storageService.decrease(order.getProductId(), order.getCount());
		log.info("------->order-service中扣减余额开始");
		// 扣余额
		accountService.decrease(order.getUserId(), order.getMoney());

		log.info("------->order-service中修改订单状态开始");
		// 改订单状态
		orderDao.updateOrder(order.getUserId(), 0);
		return ResponseVo.successMsg("订单创建成功!");
	}

}
